package com.itheima.controller;

import javax.annotation.Resource;
import javax.jms.Queue;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.itheima.dto.UserDTO;
import com.itheima.entity.Person;
import com.itheima.entity.UserInfo;
import com.itheima.enums.MsgCode;
import com.itheima.mapper.PostLikeMapper;
import com.itheima.service.UserService;
import com.itheima.utils.PageResult;
import com.itheima.utils.ResultUtils;

/**
 * @Description: 用户登录controller
 * @Date :2019-08-15 17:19
 * @Author : yuan_he_song
 */
@RestController
@RequestMapping("/user")
@EnableConfigurationProperties(Person.class)
public class UserController {

    @Resource
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private Queue queue;
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Resource
    private Person person;

    @GetMapping("test")
    public Object get(){
        return person;
    }
    /**
     * @Description: 用户登录
     * @Param: phone 用户名 password 密码
     * @Author: yuan_he_song
     * @Date: 2019-08-15 17:21
     */
    @PostMapping("/login")
    public JSONObject login(String phone, String password) {
        // 参数校验
        if (StringUtil.isEmpty(phone) || StringUtils.isEmpty(password)) {
            return ResultUtils.failureData(MsgCode.PARAM_MISSING);
        }
        UserDTO userDTO = new UserDTO();
        try {
            userDTO = userService.login(phone, password);
            if (StringUtils.isNotEmpty(userDTO.getToken())) {
                /** 登录成功 */
                return ResultUtils.successData(userDTO, MsgCode.SUCCESS.getMsg());
            }
            return ResultUtils.failureData(MsgCode.FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureData(MsgCode.LOGIN_FAIL);
        }
    }
    /**
     * 获取验证码
     */
    @GetMapping("getcode")
    public JSONObject getCode(String phone){
        userService.generateCode(phone);
        return ResultUtils.failureMsg("获取验证码成功");
    }

    /**
     * 用户注册
     */
    @PostMapping(value = "/register")
    public JSONObject register(UserInfo userInfo,String code) {
        /** 从redis中获取验证码和页面填写的进行比较 */
        String sysCode = (String) redisTemplate.boundHashOps("sms").get(userInfo.getUserPhone());
        if(!code.equals(sysCode)){
            return ResultUtils.failureMsg("验证码错误!");
        }
        try {
            userService.register(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureData(MsgCode.REGISTER_ERR);
        }
        return ResultUtils.successData(null, MsgCode.SUCCESS.getMsg());
    }

    /**
     * 退出登录
     */
    @GetMapping(value = "/logout")
    public JSONObject logout(String token, String id) {
        /** 当前用户的token */
        if (StringUtils.isEmpty(token)) {
            return ResultUtils.failureData(MsgCode.PARAM_MISSING);
        }
        String currentUser = (String) redisTemplate.boundValueOps("currentUser").get();
        String currentId = currentUser.split("-")[0];
        if (!id.equals(currentId)) {
            return ResultUtils.failureMsg("参数错误,无法退出");
        }

        RedisOperations operations = redisTemplate.boundValueOps("currentUser").getOperations();
        Boolean flag = operations.delete("currentUser");
        if (flag) {
            return ResultUtils.failureMsg("成功退出");
        }
        return ResultUtils.failureMsg("退出失败");
    }

    @Resource
    private PostLikeMapper likeMapper;

    @PostMapping("getByPage")
    public JSONObject getPage(int pageNum, int size){
        PageHelper.startPage(pageNum,size);
        Page page = (Page)likeMapper.selectByExample(null);
        PageResult result = new PageResult(page.getTotal(),page.getResult());
        return ResultUtils.successData(result,"SUCCESS");
    }

    @RequestMapping("exportAll")
    public JSONObject export(HttpServletRequest request, HttpServletResponse response)throws Exception{
        userService.export(request, response);
        return ResultUtils.successData(null,"SUCCESS");
    }
}
